<?php

/**
 * 基本控制器类
 * 95%的情况下需要模板，因此默认加载模板
 * 是否需要SESSION服务等在应用指定
 */

class controller
{
    const INIT_TEMPLATE        = 1;
    const INIT_SESSION         = 2;
    const INIT_USERLOGIN_FALSE = 4;
    const INIT_USERLOGIN_TRUE  = 8;

    static protected $role = NULL;

    static protected $view = NULL;

    /**
     * 实现权限验证，
     * 能从某个存储中找到user_id对应的权限
     */
    public static function validate($controller = CONTROLLER, $action = ACTION)
    {
        /**
         *  参考代码：从存储中取出用户权限action列表，看当前的action是否具有权限
         */
        /*
        start_sessions();
        $userGroups = User::getUserGroups($GLOBALS['_SESSION']->profile['user_id']);
        $userPrivileges = User::getUserGroupPrivileges(array_keys($userGroups));
        if (!isset($userPrivileges[$controller][$action]))
        {
            return false;
        }
         * 
         */

        return true;
    }

    public static function init($init = 0)
    {
        if ($init > 0)
        {
            if (($init & self::INIT_TEMPLATE) === self::INIT_TEMPLATE)
            {
                if (self::$view === NULL)
                {
                    require_library('framework.view');
                    self::$view = new View;
                    if (Request::getGet('compress', 'intval', 0))
                    {
                        self::$view->setEchoPolicy(Template::HTML_COMPRESSOR, false);
                    }
                }
            }

            if (($init & self::INIT_SESSION) === self::INIT_SESSION)
            {
                start_sessions();
            }
        }

        if (!self::validate())
        {
            redirect_to_login();
        }

        return true;
    }

}

?>